# This files contains your custom actions which can be used to run
# custom Python code.
#
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/core/actions/#custom-actions/


# This is a simple example for a custom action which utters "Hello World!"

# from typing import Any, Text, Dict, List
#
# from rasa_sdk import Action, Tracker
# from rasa_sdk.executor import CollectingDispatcher
#
#
# class ActionHelloWorld(Action):
#
#     def name(self) -> Text:
#         return "action_hello_world"
#
#     def run(self, dispatcher: CollectingDispatcher,
#             tracker: Tracker,
#             domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
#
#         dispatcher.utter_message("Hello World!")
#
#         return []

from typing import Any, Text, Dict, List

import json
import requests

from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
# from rasa_sdk.forms import FormAction

def post(url, data=None):
    data = json.dumps(data, ensure_ascii=False)
    data = data.encode(encoding="utf-8")
    r = requests.post(url=url, data=data)
    r = json.loads(r.text)
    return r

class ActionCharge(Action):

    def name(self) -> Text:
        return "action_charge"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        # 价格表
        price1 = {
            "奶茶":5,
            "咖啡":6,
            "卡布奇诺":10
        }
        price2 = {
            "中":1,
            "大":1.5,
            "特大":2
        }

        # 提取饮料种类，规格
        type = tracker.get_slot("type")
        size = tracker.get_slot("size")

        price = 123
        dispatcher.utter_message("您的%s杯%s一共%s元" % (size, type, price))
        
        # if size not in ['中','大','特大']:
        #     dispatcher.utter_message("不好意思，只有中杯、大杯和特大杯")
        #     return []

        # price = price1[type] * price2[size]

        # dispatcher.utter_message("您的%s杯%s一共%s元" % (size, type, price))

        return []

class ActionSearchConsume(Action):
    def name(self):
        return 'action_search_consume'

    def run(self, dispatcher, tracker, domain):
        item = tracker.get_slot("item")
        time = tracker.get_slot("time")

        dispatcher.utter_message("item:{}".format(item))
        dispatcher.utter_message("time:{}".format(time))

        if time is None:
            dispatcher.utter_message("您想查询哪个月的消费？")
            return []

        if item == "流量":
            dispatcher.utter_message(
                "您好，您{}共使用{}二百八十兆，剩余三十兆。".format(time, item))
        else:
            dispatcher.utter_message("您好，您{}共消费二十八元。".format(time))

        return []

class ActionCityTimeForm(Action):
    def name(self):
        return 'action_city_time_weather'

    def run(self, dispatcher, tracker, domain):
        city = tracker.get_slot("city")
        timec = tracker.get_slot("timec")

        dispatcher.utter_message(text='action_city_time_weather{} {}'.format(city, timec))
        
        return []

class ActionMusicLove(Action):

    def name(self) -> Text:
        return "action_musiclove"

    def run(self, dispatcher, tracker, domain):

        dispatcher.utter_message(text='action_musiclove')

        return []

class ActionMusicPlay(Action):

    def name(self) -> Text:
        return "action_musicplay"

    def run(self, dispatcher, tracker, domain):

        dispatcher.utter_message(text='action_musicplay')

        return []

class ActionMusicStop(Action):

    def name(self) -> Text:
        return "action_musicstop"

    def run(self, dispatcher, tracker, domain):

        dispatcher.utter_message(text='action_musicstop')

        return []

class ActionMusicPause(Action):

    def name(self) -> Text:
        return "action_musicpause"

    def run(self, dispatcher, tracker, domain):

        dispatcher.utter_message(text='action_musicpause')

        return []

class ActionMusicGoOn(Action):

    def name(self) -> Text:
        return "action_musicgoon"

    def run(self, dispatcher, tracker, domain):

        dispatcher.utter_message(text='action_musicgoon')

        return []

class ActionMusicNext(Action):

    def name(self) -> Text:
        return "action_musicnext"

    def run(self, dispatcher, tracker, domain):

        dispatcher.utter_message(text='action_musicnext')

        return []

class ActionMusicBefore(Action):

    def name(self) -> Text:
        return "action_musicbefore"

    def run(self, dispatcher, tracker, domain):

        dispatcher.utter_message(text='action_musicbefore')

        return []

class ActionMusicVolumeAdd(Action):

    def name(self) -> Text:
        return "action_musicvolumeadd"

    def run(self, dispatcher, tracker, domain):

        dispatcher.utter_message(text='action_musicvolumeadd')

        return []

class ActionMusicVolumeMinus(Action):

    def name(self) -> Text:
        return "action_musicvolumeminus"

    def run(self, dispatcher, tracker, domain):

        dispatcher.utter_message(text='action_musicvolumeminus')

        return []

class ActionMusicVolumeMute(Action):

    def name(self) -> Text:
        return "action_musicvolumemute"

    def run(self, dispatcher, tracker, domain):

        dispatcher.utter_message(text='action_musicvolumemute')

        return []